import axios from "axios";

// window.RPConfig

const interceptorsRequest = [
  function(config) {
    // Do something before request is sent
    return config;
  },
  function(error) {
    // Do something with request error
    return Promise.reject(error);
  }
];

const interceptorsResponse = [
  function(response) {
    // Do something with response data
    return response;
  },
  function(error) {
    // Do something with response error
    return Promise.reject(error);
  }
];

class Request {
  constructor(env) {
    this.baseURL = window.RPConfig[env];
  }
  create(common = '') {
    const instance = axios.create({
      baseURL: this.baseURL + common,
      timeout: 1000,
      headers: { "X-Custom-Header": "foobar" }
    });

    // Add a request interceptor
    instance.interceptors.request.use(...interceptorsRequest);

    // Add a response interceptor
    instance.interceptors.response.use(...interceptorsResponse);
    return instance
  }
}

export default Request;
